package com.feng.gaoerfu.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.feng.gaoerfu.R;
import com.feng.gaoerfu.base.BaseActivity;
import com.feng.gaoerfu.base.BaseFragment;
import com.feng.gaoerfu.fragment.GameFragment;
import com.feng.gaoerfu.fragment.InteractiveFragment;
import com.feng.gaoerfu.fragment.ScroingFragment;
import com.feng.gaoerfu.view.ControlScrollViewPager;
import com.feng.gaoerfu.view.MenuDialog;
import com.feng.gaoerfu.view.StatusBarCompat;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {


    private static final int SCROING = 0;
    private static final int INTERACTIVE = 1;
    private static final int GAME = 2;
    @InjectView(R.id.tv_back)
    TextView mTvBack;
    @InjectView(R.id.iv_menu)
    ImageView mIvMenu;
    @InjectView(R.id.rb_jifen)
    RadioButton mRbJifen;
    @InjectView(R.id.rb_hudong)
    RadioButton mRbHudong;
    @InjectView(R.id.rb_game)
    RadioButton mRbGame;
    @InjectView(R.id.main_rg)
    RadioGroup mMainRg;
    @InjectView(R.id.view_pager)
    ControlScrollViewPager mViewPager;
    public SparseArray<BaseFragment> mFragments = new SparseArray<>();
    private MyViewpagerAdapter mAdapter;
    private BaseFragment mFragment;
    private MenuDialog mMenuDialog;

    @Override
    public int initContentView() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        ButterKnife.inject(this);
        StatusBarCompat.setColorNoTranslucent(this, getResources().getColor(R.color.text_black));
//        mIvMenu.setVisibility(View.VISIBLE);
    }

    @Override
    public void initData() {
        mRbJifen.setChecked(true);

        mAdapter = new MyViewpagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mAdapter);
        mViewPager.setCanScroll(false);
        mViewPager.setOffscreenPageLimit(2);
        List<String> list = new ArrayList<>();
        list.add("完成比赛并确认成绩");
        list.add("查看成绩卡");
        list.add("选择球童计分");
        list.add("修改半场");
        list.add("修改T台");
        list.add("修改比赛");
        list.add("取消比赛");
        list.add("取消");
        mMenuDialog = new MenuDialog.Builder(mContext).setList(list).build();
        mMenuDialog.setCancelable(true);
    }


    @Override
    public void initListener() {

    }

    @OnClick({R.id.tv_back, R.id.iv_menu, R.id.rb_jifen, R.id.rb_hudong, R.id.rb_game})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_back:
                finish();
                break;
            case R.id.iv_menu:
                mMenuDialog.show();
                break;
            case R.id.rb_jifen:
                mViewPager.setCurrentItem(SCROING);
                break;
            case R.id.rb_hudong:
                mViewPager.setCurrentItem(INTERACTIVE);
                break;
            case R.id.rb_game:
                mViewPager.setCurrentItem(GAME);
                break;
        }
    }


    private class MyViewpagerAdapter extends FragmentPagerAdapter {

        public MyViewpagerAdapter(FragmentManager fm) {
            super(fm);
        }

        //FragmentPagerAdapter仅在创建新的fragment时候调用
        @Override
        public Fragment getItem(int position) {
            return createFragment(position);
        }

        @Override
        public int getCount() {
            return 3;
        }
    }

    private Fragment createFragment(int position) {
        mFragment = mFragments.get(position);

        if (mFragment != null) {
            return mFragment;
        }

        switch (position) {
            case SCROING:
                mFragment = new ScroingFragment();
                break;
            case INTERACTIVE:
                mFragment = new InteractiveFragment();
                break;
            case GAME:
                mFragment = new GameFragment();
                break;

        }
        mFragments.put(position, mFragment);
        return mFragment;
    }
}
